<?php
require_once '../../a_config.php';
require_once "{$A_CONFIG['api_include']}";
require_once '../../api_header.php';
$conn = connect_to_db();
$err = -1;
$msg = "";
$page_num = 10;//每页显示数量
$condition = array();
$aa="";
if (isset($_POST['token']) && !empty($_POST["token"]) &&
    isset($_POST['time']) && !empty($_POST["time"]) &&
    isset($_POST["username"]) && !empty($_POST["username"]) &&
    isset($_POST["passwd"]) && !empty($_POST["passwd"])
) {
    $key = $A_CONFIG['api_key'];
    $unixtime = $_POST['time'];
    $token = md5($key . $unixtime);
    if ($token == $_POST["token"]) {
        $username = mysql_real_escape_string($_POST["username"]);
        $passwd = mysql_real_escape_string($_POST["passwd"]);
        if( isset($_POST["auth"]) && !empty($_POST["auth"]) ){
            $auth =mysql_real_escape_string($_POST["auth"]);
            $auth_arr = explode(",", $auth);
            foreach ($auth_arr as $k=>$item){
                $aa=$aa." , ".$item." =1 ";
            }
        }
        $where = "where is_del=0 and username='$username' limit 1";
        $s_db_name = $A_CONFIG['project_pre'] . '_backstage_admin_list';
        $sql = "SELECT id FROM {$s_db_name} $where";
        $res = mysql_query($sql, $conn);
        if (is_resource($res) && mysql_num_rows($res) == 0) {
            $sql = "insert into {$s_db_name} set `username`='$username',`password`='$passwd',create_time=now(),update_time=now() $aa";
            $res = mysql_query($sql, $conn);
            if ($res) {
                $msg = "添加成功";
                $err = 0;
            } else {
                $err = -2;
                $msg = "数据处理失败，请重试";
            }
        } else {
            $err = -3;
            $msg = "该账号已经存在";
        }
    } else {
        $err = -4;
        $msg = "数据处理出错";

    }


} else {
    $err = -5;
    $msg = "参数错误";
}
if (!$A_CONFIG['is_debug']) {
    $resArr = compact("err", "msg");
} else {
    $resArr = compact("err", "msg", 'sql',"aa");
}
ob_clean();
$resJson = json_encode_cn($resArr);
echo $resJson;
